package top.kaoshanji.q1ch07a.web;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import top.kaoshanji.q1ch07a.exception.AppWideException;
import top.kaoshanji.q1ch07a.exception.DuplicateException;
import top.kaoshanji.q1ch07a.exception.NotFound404Exception;
import top.kaoshanji.q1ch07a.model.Spittle;

/**
 * @author kaoshanji
 * ^_^
 * create time 2025/4/25 15:06
 */
@Controller
@RequestMapping("/spittle")
public class SpittleController {

    /**
     * 代码清单-q1ch07a-03：这是一个资源没有找到的异常404HTTP状态码
     * @param spittleId
     * @param model
     * @return
     */
    @RequestMapping(value = "/spittleDetail/{spittleId}", method = RequestMethod.GET)
    public String spittleDetail(@PathVariable("spittleId") long spittleId, Model model) {

        Spittle spittle = null;
        if (spittle == null) {
            throw new NotFound404Exception();
        }

        return "spittle";
    }

    /**
     * 代码清单-q1ch07a-04：如果此控制器内的方法抛出DuplicateException异常，就会被handleDuplicate方法处理
     * @param model
     * @return
     */
    @RequestMapping(value = "/saveSpittle", method = RequestMethod.GET)
    public String saveSpittle(Model model) {

        Spittle spittle = null;
        if (spittle == null) {
            throw new DuplicateException();
        }

        return "spittle";
    }

    /**
     * 代码清单-q1ch07a-05：如果全局范围内的控制器抛出AppWideException，就会被AppWideExceptionHandler处理
     * @param model
     * @return
     */
    @RequestMapping(value = "/spittleList", method = RequestMethod.GET)
    public String spittleList(Model model) {

        Spittle spittle = null;
        if (spittle == null) {
            throw new AppWideException();
        }

        return "spittle";
    }

    /**
     * 代码清单-q1ch07a-06：处理此控制器内抛出的DuplicateException异常
     * @return 逻辑视图名
     */
    @ExceptionHandler(DuplicateException.class)
    public String handleDuplicate() {
        return "error/duplicateException";
    }



}
